package Leetcode.DayTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName Test_425
 * @since: 2024/11/24 10:28
 * @auth: kirito
 * @description: 周赛425
 **/
public class Test_425 {
    public static void main(String[] args) {
        Test_425 test = new Test_425();
//        String s1 = "aabbcc";
//        String s2 = "bbaacc";
//        System.out.println(test.isPossibleToRearrange(s1, s2, 3));
//        int num = 5;
//        //向上取整
//        int result = (num + 1) >> 1;
//        System.out.println(result);

        int[] arr = {2, 4, 3};
        System.out.println(test.minArraySum(arr, 3, 2, 1));

    }
    public int minArraySum(int[] nums, int k, int op1, int op2) {
        int ans = 0;
        int len = nums.length;
        Arrays.sort(nums);

        for (int i = len-1; i >=0; i--) {
            int temp1 = nums[i];
            int temp2 = nums[i];
            if (op1 > 0) {
                temp1 = (temp1 + 1) >> 1;
            }
            if (op2 > 0) {
                temp2 -= k;
            }

            if (temp1 < temp2) {
                op1--;
                nums[i] = temp1;
            } else {
                op2--;
                nums[i] = temp2;
            }
            ans += nums[i];

        }
        return ans;
    }

    public boolean isPossibleToRearrange(String str, String t, int k) {
        Map<String, Integer> strMap = new HashMap<>();
        Map<String, Integer> tMap = new HashMap<>();

        k = str.length() / k;
        // 按照长度为k进行切片
        for (int i = 0; i < str.length(); i += k) {
            String slice = str.substring(i, i + k);
            String slice2 = t.substring(i, i + k);
            strMap.put(slice, strMap.getOrDefault(slice, 0) + 1);
            tMap.put(slice2, tMap.getOrDefault(slice2, 0) + 1);
        }
        for (Map.Entry<String, Integer> entry : strMap.entrySet()) {
            String key = entry.getKey();
            int count = entry.getValue();
            if (count != tMap.getOrDefault(key, 0)) {
                return false;
            }
        }
        return true;
    }

    public int minimumSumSubarray(List<Integer> nums, int l, int r) {
        int minSum = Integer.MAX_VALUE;
        boolean found = false;

        // 遍历数组，对于每个起始点i，计算长度在l和r之间的子数组的和
        for (int i = 0; i <= nums.size() - l; i++) {
            for (int len = l; len <= r && i + len <= nums.size(); len++) {
                int sum = 0;
                // 计算从i开始长度为len的子数组的和
                for (int j = i; j < i + len; j++) {
                    sum += nums.get(j);
                }
                // 如果和大于0且小于当前最小和，则更新最小和
                if (sum > 0 && sum < minSum) {
                    minSum = sum;
                    found = true;
                }
            }
        }

        // 如果找到了大于0的最小和，则返回它，否则返回-1
        return found ? minSum : -1;
    }
}
